<template>
    <!--  轮播图-->
    <div >

      <el-carousel height="150px">
        <el-carousel-item v-for="item in images" :key="item">
          <img :src="item" :alt="`Image ${index + 1}`" class="carousel-image" />
        </el-carousel-item>
      </el-carousel>

    </div>


  <div class="top-bar">
    <el-button type="primary" @click="toggleLogin" class="login-button">登录</el-button>
  </div>

  <!-- 登录表单 -->
  <div class="login-container" v-if="showLogin">
    <el-card class="login-card">
      <h2 class="login-title">登录</h2>
      <el-form :model="form" ref="formRef" class="login-form">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password" label="密&nbsp&nbsp&nbsp&nbsp码">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <!-- 验证码部分 -->
        <el-form-item label="验证码">
          <div class="captcha-container">
            <!-- 输入框在左 -->
            <el-input v-model="form.captcha" placeholder="请输入验证码" class="captcha-input" />
            <!-- 验证码图片在右 -->
            <img v-if="captchaUrl" :src="captchaUrl" alt="Captcha" @click="fetchCaptcha" class="captcha-img" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { login, fetchCaptchaAPI } from '@/api/LoginAndRegistration.js'
import router from "@/router/index.js";

const form = ref({
  username: '',
  password: '',
  captcha: '' // 添加验证码输入字段
})
const images = ref([
  'src/assets/Photo/01.jpg',
  'src/assets/Photo/02.jpg',
  'src/assets/Photo/03.jpg',
  'src/assets/Photo/04.jpg',
])

const formRef = ref(null)
const userStore = useUserStore()
const showLogin = ref(false) // 控制登录表单的显示隐藏

// 用于存储验证码图片的 URL
const captchaUrl = ref('')

// 页面加载时获取验证码
const fetchCaptcha = () => {
  console.log('Fetching captcha...')
  fetchCaptchaAPI().then((url) => {
    captchaUrl.value = url
    console.log('Captcha URL:', url)
  }).catch((err) => {
    ElMessage.error('Failed to load captcha')
  })
}

// 登录操作
const handleLogin = () => {
  login(form.value).then((res) => {
    if (res.data.code === 200) {
      userStore.setUserId(res.data.data.userId)
      userStore.setToken(res.data.data.token)
      ElMessage.success('Login successful!')
      router.push('/layout')
    } else {
      ElMessage.error(res.message)
    }
  }).catch((err) => {
    ElMessage.error(err.message)
  })
}

// 切换登录表单的显示
const toggleLogin = () => {
  showLogin.value = !showLogin.value
}

// 页面加载时自动获取验证码
fetchCaptcha()
</script>

<style scoped>
/* 轮播图样式 */

/* 顶部登录按钮样式 */
.top-bar {
  position: relative;
  top: 20px;
  right: 20px;
  z-index: 100;
}

.login-button {
  font-size: 16px;
}

/* 登录表单的整体样式 */
.login-container {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.9);
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

.login-card {
  width: 400px;
  padding: 20px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  font-weight: bold;
}

.login-form {
  display: flex;
  flex-direction: column;
}

/* 验证码容器样式 */
.captcha-container {
  display: flex;
  align-items: center;
}

/* 验证码输入框样式 */
.captcha-input {
  flex: 1;
  margin-right: 10px;
}

/* 验证码图片样式 */
.captcha-img {
  width: 100px;
  height: 30px;
  border: 1px solid #ccc;
  cursor: pointer;
}
</style>
